package cn.sc.summer.core.config;

import cn.sc.summer.core.interceptor.RequestInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.lang.NonNull;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.servlet.config.annotation.*;

/**
 * webmvc配置项
 */
@Slf4j
@Configuration
@EnableScheduling
public class WebMvcConfig extends WebMvcConfigurationSupport {

    @Override
    public void addResourceHandlers(@NonNull ResourceHandlerRegistry registry) {
    }

    @Override
    public void addInterceptors(InterceptorRegistry interceptor) {
        RequestInterceptor requestInterceptor = new RequestInterceptor();
        interceptor.addInterceptor(requestInterceptor).addPathPatterns("/**");
    }

    @Override
    public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
        configurer.defaultContentType(MediaType.APPLICATION_JSON);
    }

    /**
     * 跨域支持
     *
     * @author a-xin
     * @date 2021/6/30 9:55
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOriginPatterns("*")
                .allowCredentials(true)
                .allowedMethods("GET", "POST", "DELETE", "PUT", "PATCH", "OPTIONS", "HEAD")
                .maxAge(3600 * 24);
    }

}
